﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Honestysoft.BaseObject;
using Honestysoft.Financing.Model;
using Honestysoft.Baseframework.WebUI;
using Honestysoft.BaseLibrary.ExceptionHandling;
using System.Data;
using Honestysoft.Baseframework.Model;
using System.Configuration;
using Honestysoft.DictionaryHelper;

namespace Honestysoft.Financing.WebUI.Module.Project
{
    public partial class BD_Project_List2 : BasePage
    {
        protected override void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            if (!IsPostBack)
            {
                AspNetPager1.PageSize = Globals.PageSize;
                BindControl();
                Bind();
            }
        }
        /// <summary>
        /// 绑定页面控件。
        /// </summary>
        private void BindControl()
        {           
            //QueryParameters query = new QueryParameters();
            //query.Add("CategoryID", ConfigurationManager.AppSettings["CompanyCategory_XM"], QueryParameters.OperatorType.Equal);
            //DataTable dt = new BaseBusinessLogic<Org_Company>(Globals.CurrentConnStringName).GetTable(query);
            //ddlProjectCategory.DataSource = dt;
            //ddlProjectCategory.DataTextField = "CompName";
            //ddlProjectCategory.DataValueField = "ID";
            //ddlProjectCategory.DataBind();
            //ddlProjectCategory.Items.Insert(0, new ListItem("---全部---", "0"));
            //QueryParameters query = new QueryParameters();
            //query.Add("CategoryID", "ProjectCategory", QueryParameters.OperatorType.Equal);
            //OrderParameters order = new OrderParameters();
            //order.Add("SortCode");
            //DataTable dt = new BaseBusinessLogic<Sys_Dictionary>(Globals.CurrentConnStringName).GetTable(query, order);
            //ddlProjectCategory.DataSource = dt;
            //ddlProjectCategory.DataTextField = "DictName";
            //ddlProjectCategory.DataValueField = "DictValue";
            //ddlProjectCategory.DataBind();
            ControlHelper.BindDictionary(ddlProjectCategory, "ProjectCategory");
            ControlHelper.InsertAllItemAndSelect(ref ddlProjectCategory);
            //if (Request.QueryString["ID"] != "0")
            //{
            //    ddlProjectCategory.Enabled = false;
            //    ddlProjectCategory.SelectedValue = Request.QueryString["ID"];
            //}
        }
        /// <summary>
        /// 设置弹出窗体大小
        /// 基类中已自动做控制。默认宽度800，高度600
        /// </summary>
        protected override void SetDialogSize()
        {
            //base.SetDialogSize();
            //重载宽度，高度
            ModalDialogSize.EditHeight = 600;
            ModalDialogSize.EditWidth = 720;
            ModalDialogSize.ViewHeight = 600;
            ModalDialogSize.ViewWidth = 720;
        }

        protected string GetEditUrl(string subject, string id)
        {
            string url = "";
            //url += ScriptHelper.LinkButton.GetGridEditLink(subject, Globals.Home + "Module/Project/BD_Project_Edit.aspx", "修改项目信息", ModalDialogSize.EditWidth, ModalDialogSize.EditHeight, id, FunctionID);
            url += ScriptHelper.LinkButton.GetGridEditLink(subject, Globals.Home + "Module/Project/Project_Edit.aspx", "修改项目信息", ModalDialogSize.EditWidth, ModalDialogSize.EditHeight, id, FunctionID);
            return url;
        }
        private int _theYear = 2010;
        protected int TheYear
        {
            get
            {
                if (_theYear > DateTime.Today.Year) { _theYear = 2010; }
                return _theYear++;
            }
        }

        //DataTable dtProject = null;

        //protected int sYear = 2011;
        //protected int eYear = DateTime.Today.Year;

        //protected string GetPlanColumnHead()
        //{

        //    //if (dtProject == null) { return ""; }
        //    //string colString = "";
        //    //string colName="";
        //    //foreach (DataColumn dc in dtProject.Columns)
        //    //{
        //    //    colName=dc.ColumnName;
        //    //    if (colName.StartsWith("planYear"))
        //    //    {
        //    //        colString += "<th align=\"center\" width=\"80px\">" + colName.Substring(colName.Length - 4) + "年度<br/>投资计划</th>";
        //    //    }
        //    //}
        //    //return colString;
        //}

        protected string GetPlanColumnData(object obj)
        {
            DataRowView drv = obj as DataRowView;
            if (drv == null) { return ""; }
            string colString = "";
            string colName = "";
            foreach (DataColumn dc in drv.DataView.Table.Columns)
            {
                colName = dc.ColumnName;
                if (colName.StartsWith("planYear"))
                {
                    colString += "<td style=\"text-align:right;font-family:Arial Narrow\">" + string.Format("{0:N}", drv[colName]) + "</td>";
                }
            }
            return colString;
        }

        /// <summary>
        /// 根据权限控制页面按钮.
        /// 基类中已自动做控制。
        /// </summary>
        protected override void ButtonSetting()
        {
            base.ButtonSetting();
            //使用PageSecurity检查权限
            if (PageSecurity.Add)
            {
                btnAdd.Attributes.Add("onclick", "javascript:openDialog('" + Globals.Home + "Module/Project/Project_Edit.aspx?FunctionID=" + FunctionID + "'," + ModalDialogSize.EditWidth + ", " + ModalDialogSize.EditHeight + ", '新增项目信息');return false;");
            }
        }
        /// <summary>
        /// 绑定列表
        /// </summary>
        private void Bind()
        {
            string owner971 = "";
            if (ddlProjectCategory.SelectedValue != "0")
            {
                owner971 = ddlProjectCategory.SelectedValue;
            }
            int recordCount = 0;
            DataTable dtProject = new Honestysoft.Financing.BusinessLogic.Project(Globals.CurrentConnStringName).QueryProjects(txtSN.Text.Trim(), owner971, txtName.Text.Trim(), AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, out recordCount);
            AspNetPager1.RecordCount = recordCount;
            rptList.DataSource = dtProject;
            rptList.DataBind();
        }
        protected string GetPlanYearString(int year,object obj)
        {
            DataRowView drv = obj as DataRowView;
            return string.Format("{0:N}", drv["planYear" + year.ToString()]);
        }

        protected string GetCheckbox(string dataId, bool isInit,bool isUse)
        {
            if (isUse) { return ""; }
            return "<input type=\"checkbox\" name=\"chkID\" id=\"chk" + dataId + "\"  value=\"" + dataId + "\" />";
        }

        /// <summary>
        /// 得到表格行操作按钮脚本
        /// </summary>
        /// <param name="dataId">记录ID</param>
        /// <param name="isInit">是否已初始化</param>
        /// <returns>操作按钮脚本</returns>
        protected string GetButtonScript(string dataId, bool isInit,bool isUse)
        {
            string script = "";
            if (PageSecurity.Edit)
            {
                //script += ScriptHelper.LinkButton.GetGridEditButton(Globals.Home + "Module/Project/BD_Project_Edit.aspx", "修改项目信息", ModalDialogSize.EditWidth, ModalDialogSize.EditHeight, dataId, FunctionID);
                script += ScriptHelper.LinkButton.GetGridEditButton(Globals.Home + "Module/Project/Project_Edit.aspx", "修改项目信息", ModalDialogSize.EditWidth, ModalDialogSize.EditHeight, dataId, FunctionID);
            }
            if (PageSecurity.Delete && !isUse)
            {
                if (script.Length > 0) { script += "&nbsp;"; }
                script += ScriptHelper.LinkButton.GetGridDeleteButton(dataId, btnDelete.ClientID);
            }
            //if (PageSecurity.View)
            //{
            //    if (script.Length > 0) { script += "&nbsp;"; }
            //    script += ScriptHelper.LinkButton.GetGridViewButton(Globals.Home + "Module/Project/BD_Project_View.aspx", "查看项目信息", ModalDialogSize.ViewWidth, ModalDialogSize.ViewHeight, dataId, FunctionID);
            //}
            return script;
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            Bind();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            Bind();
        }
        /// <summary>
        /// 得到选中的记录ID集合
        /// </summary>
        /// <returns></returns>
        private string[] GetSelectedRowKeys()
        {
            string strId = Request["chkID"] + "";
            if (string.IsNullOrEmpty(strId)) { return null; }
            return strId.Split(',');
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                string[] arrId = GetSelectedRowKeys();
                BaseBusinessLogic<BD_Project> baseLogic = new BaseBusinessLogic<BD_Project>();
                baseLogic.DeleteByKey(arrId);
                ScriptHelper.Window.Alert("删除成功！");
                Bind();
            }
            catch (Honestysoft.BaseLibrary.ExceptionHandling.BaseException bex)
            {
                ExceptionHelper.ExceptionDistribute(bex, false);
                ScriptHelper.Window.Alert(bex.Message);
            }
            catch (Exception ex)
            {
                ExceptionHelper.ExceptionDistribute(ex, false);
                ScriptHelper.Window.Alert("删除失败！系统发生了未知错误！");
            }
        }
    }
}